﻿Public Class wAdmin

    Private Sub wAdmin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        wPantalla1.Show()
        txtNuevaBolilla.Focus()
    End Sub

    Private Function validarBolilla() As Boolean
        Try
            txtNuevaBolilla.Text = txtNuevaBolilla.Text.Replace(".", "").Replace(",", "").Replace("+", "").Trim()
            Dim num As UInteger = txtNuevaBolilla.Text
            Dim arr = txtHistorial.Text.Split(",")
            For Each c In txtNuevaBolilla.Text
                Dim a = "0123456789"
                If Not a.Contains(c) Then Return False
            Next
            If num < 1 OrElse num > 90 OrElse arr.Contains(txtNuevaBolilla.Text) Then
                Return False
            End If

        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function

    Private Function validarBolillaAVerificar() As Boolean
        Try
            txtVerificarBolilla.Text = txtVerificarBolilla.Text.Replace(".", "").Replace(",", "").Replace("+", "").Replace("$", "").Trim()
            Dim num As UInteger = txtVerificarBolilla.Text
            For Each c In txtVerificarBolilla.Text
                Dim a = "0123456789"
                If Not a.Contains(c) Then Return False
            Next
            Dim arr = txtHistorial.Text.Split(",")
            If num < 1 OrElse num > 90 Then
                Return False
            End If

        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function

    Public Sub actualizarHistorial()
        Dim coma = ""
        If Not String.IsNullOrWhiteSpace(txtHistorial.Text) Then coma = ","
        txtHistorial.Text += coma & txtNuevaBolilla.Text
    End Sub

    Private Sub actualizarHistorialVerificacion()
        Dim coma = ""
        If Not String.IsNullOrWhiteSpace(txtHistorialVerificacion.Text) Then coma = ","
        txtHistorialVerificacion.Text += coma & txtVerificarBolilla.Text
    End Sub

    Public Sub borrarTablero()
        txtHistorial.Text = ""
        actualizarTablero()
    End Sub

    Public Sub borrarBolillaVerificada()
        Dim arr = txtHistorialVerificacion.Text.Split(",")
        Array.Resize(arr, arr.Count - 1)
        txtHistorialVerificacion.Text = ""
        For Each i In arr
            txtHistorialVerificacion.Text += IIf(txtHistorialVerificacion.Text = "", "", ",") & i
        Next
        actualizarTablero()
    End Sub

    Private Sub borrarBolilla()
        Dim arr = txtHistorial.Text.Split(",")
        Array.Resize(arr, arr.Count - 1)
        txtHistorial.Text = ""
        For Each i In arr
            txtHistorial.Text += IIf(txtHistorial.Text = "", "", ",") & i
        Next
        actualizarTablero()
    End Sub

    Public Sub actualizarTablero()
        wPantalla1.lblVerificando.Location = New Point(200, 50)
        Dim arr = txtHistorial.Text.Split(",")
        Dim arrVerif = txtHistorialVerificacion.Text.Split(",")
        For Each ctrl In panelTablero.Controls
            Dim celda = TryCast(ctrl, TextBox)
            If arr.Contains(celda.Text) Then
                celda.BackColor = Color.Yellow
                celda.ForeColor = Color.Black

            Else
                celda.BackColor = Color.White
                celda.ForeColor = Color.White
            End If

        Next
        For Each ctrl In wPantalla1.panelTablero.Controls
            Dim celda = TryCast(ctrl, TextBox)
            If arr.Contains(celda.Text) Then

                celda.BackColor = Color.Yellow
                celda.ForeColor = Color.Black
            Else
                celda.BackColor = Color.White
                celda.ForeColor = Color.White
            End If
        Next
        Dim n = IIf(arr.Count = 1 And arr(0) = "", 0, arr.Count)
        If Not txtHistorial.Text.Trim = "" Then
            txtBolillaCantada.Text = arr.Last
            txtBolillaCantada.BackColor = Color.Yellow
        Else
            txtBolillaCantada.Text = ""
            txtBolillaCantada.BackColor = Color.White
        End If



        If n > 1 Then txtBolillaAnt1.Text = arr(n - 2) Else txtBolillaAnt1.Text = ""
        If n > 2 Then txtBolillaAnt2.Text = arr(n - 3) Else txtBolillaAnt2.Text = ""
        If n > 3 Then txtBolillaAnt3.Text = arr(n - 4) Else txtBolillaAnt3.Text = ""
        If n > 4 Then txtBolillaAnt4.Text = arr(n - 5) Else txtBolillaAnt4.Text = ""
        If n > 5 Then txtBolillaAnt5.Text = arr(n - 6) Else txtBolillaAnt5.Text = ""

        wPantalla1.txtBolillaAnt5.Text = txtBolillaAnt5.Text
        wPantalla1.txtBolillaAnt4.Text = txtBolillaAnt4.Text
        wPantalla1.txtBolillaAnt3.Text = txtBolillaAnt3.Text
        wPantalla1.txtBolillaAnt2.Text = txtBolillaAnt2.Text
        wPantalla1.txtBolillaAnt1.Text = txtBolillaAnt1.Text

        wPantalla1.txtBolillaCantada.Text = txtBolillaCantada.Text
        wPantalla1.txtBolillaCantada.BackColor = txtBolillaCantada.BackColor

        wPantalla1.lblVerificando.Visible = chkVerificar.Checked

        wPantalla1.txtBolillaCantada.Visible = Not chkVerificar.Checked
        wPantalla1.Label1.Visible = Not chkVerificar.Checked
        wPantalla1.Label2.Visible = Not chkVerificar.Checked
        wPantalla1.Label3.Visible = Not chkVerificar.Checked
        wPantalla1.txtBolillaAnt5.Visible = Not chkVerificar.Checked
        wPantalla1.txtBolillaAnt4.Visible = Not chkVerificar.Checked
        wPantalla1.txtBolillaAnt3.Visible = Not chkVerificar.Checked
        wPantalla1.txtBolillaAnt2.Visible = Not chkVerificar.Checked
        wPantalla1.txtBolillaAnt1.Visible = Not chkVerificar.Checked
        wPantalla1.panelTablero.Location = New Point(4, 46)

        If chkVerificar.Checked Then
            wPantalla1.panelTablero.Location = New Point(200, 90)

            For Each ctrl In panelTablero.Controls
                Dim celda = TryCast(ctrl, TextBox)
                celda.BackColor = Color.White
                If txtHistorial.Text.Split(",").Contains(celda.Text) Then celda.ForeColor = Color.Gray
            Next
            For Each ctrl In wPantalla1.panelTablero.Controls
                Dim celda = TryCast(ctrl, TextBox)
                celda.BackColor = Color.White
                If txtHistorial.Text.Split(",").Contains(celda.Text) Then celda.ForeColor = Color.Gray
            Next


            For Each ctrl In panelTablero.Controls
                Dim celda = TryCast(ctrl, TextBox)
                If arrVerif.Contains(celda.Text) Then
                    If arr.Contains(celda.Text) Then
                        celda.BackColor = Color.Lime
                        celda.ForeColor = Color.Black

                    Else
                        celda.BackColor = Color.Black
                        celda.ForeColor = Color.Maroon

                    End If
                End If
            Next

            For Each ctrl In wPantalla1.panelTablero.Controls
                Dim celda = TryCast(ctrl, TextBox)
                If arrVerif.Contains(celda.Text) Then
                    If arr.Contains(celda.Text) Then
                        celda.BackColor = Color.Lime
                        celda.ForeColor = Color.Black

                    Else
                        celda.BackColor = Color.Black
                        celda.ForeColor = Color.Maroon

                    End If
                End If
            Next
        End If
    End Sub

    Private Sub btProyectar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btProyectar.Click
        If wPantalla1.Visible Or wPantalla2.Visible Then
            wPantalla1.Visible = False
            wPantalla2.Visible = False
        Else
            wPantalla1.Visible = RadioButton1.Checked
            wPantalla2.Visible = RadioButton2.Checked
        End If
        actualizarTablero()
    End Sub

    Private Sub txtNuevaBolilla_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNuevaBolilla.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            If validarBolilla() Then
                actualizarHistorial()
                actualizarTablero()
            End If
            txtNuevaBolilla.Text = ""
        End If
    End Sub

    Private Sub chkBorrarBolilla_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBorrarBolilla.CheckedChanged
        btBorrarBolilla.Enabled = chkBorrarBolilla.Checked
    End Sub

    Private Sub chkBorrarTablero_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBorrarTablero.CheckedChanged
        btBorrarTablero.Enabled = chkBorrarTablero.Checked
    End Sub

    Private Sub btBorrarTablero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btBorrarTablero.Click
        borrarTablero()
        chkBorrarTablero.Checked = False
    End Sub

    Private Sub btBorrarBolilla_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btBorrarBolilla.Click
        borrarBolilla()
        chkBorrarBolilla.Checked = False
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged

        RadioButton1.Checked = Not RadioButton2.Checked
        wPantalla1.Visible = RadioButton1.Checked
        wPantalla2.Visible = RadioButton2.Checked
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged

        RadioButton2.Checked = Not RadioButton1.Checked
        wPantalla1.Visible = RadioButton1.Checked
        wPantalla2.Visible = RadioButton2.Checked
    End Sub

    Private Sub TextBoxesTablero_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBolillaCantada.Enter, txtBolillaAnt5.Enter, txtBolillaAnt4.Enter, txtBolillaAnt3.Enter, txtBolillaAnt2.Enter, txtBolillaAnt1.Enter, TextBox90.Enter, TextBox9.Enter, TextBox89.Enter, TextBox88.Enter, TextBox87.Enter, TextBox86.Enter, TextBox85.Enter, TextBox84.Enter, TextBox83.Enter, TextBox82.Enter, TextBox81.Enter, TextBox80.Enter, TextBox8.Enter, TextBox79.Enter, TextBox78.Enter, TextBox77.Enter, TextBox76.Enter, TextBox75.Enter, TextBox74.Enter, TextBox73.Enter, TextBox72.Enter, TextBox71.Enter, TextBox70.Enter, TextBox7.Enter, TextBox69.Enter, TextBox68.Enter, TextBox67.Enter, TextBox66.Enter, TextBox65.Enter, TextBox64.Enter, TextBox63.Enter, TextBox62.Enter, TextBox61.Enter, TextBox60.Enter, TextBox6.Enter, TextBox59.Enter, TextBox58.Enter, TextBox57.Enter, TextBox56.Enter, TextBox55.Enter, TextBox54.Enter, TextBox53.Enter, TextBox52.Enter, TextBox51.Enter, TextBox50.Enter, TextBox5.Enter, TextBox49.Enter, TextBox48.Enter, TextBox47.Enter, TextBox46.Enter, TextBox45.Enter, TextBox44.Enter, TextBox43.Enter, TextBox42.Enter, TextBox41.Enter, TextBox40.Enter, TextBox4.Enter, TextBox39.Enter, TextBox38.Enter, TextBox37.Enter, TextBox36.Enter, TextBox35.Enter, TextBox34.Enter, TextBox33.Enter, TextBox32.Enter, TextBox31.Enter, TextBox30.Enter, TextBox3.Enter, TextBox29.Enter, TextBox28.Enter, TextBox27.Enter, TextBox26.Enter, TextBox25.Enter, TextBox24.Enter, TextBox23.Enter, TextBox22.Enter, TextBox21.Enter, TextBox20.Enter, TextBox2.Enter, TextBox19.Enter, TextBox18.Enter, TextBox17.Enter, TextBox16.Enter, TextBox15.Enter, TextBox14.Enter, TextBox13.Enter, TextBox12.Enter, TextBox11.Enter, TextBox10.Enter, TextBox1.Enter
        txtNuevaBolilla.Focus()
    End Sub

    Private Sub chkVerificar_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkVerificar.CheckedChanged
        Dim b = chkVerificar.Checked
        lblVErificarBolilla.Enabled = b
        txtVerificarBolilla.Enabled = b
        chkBorrarAntVerif.Enabled = b
        chkBorrarVerif.Enabled = b
        btBorrarAntVerif.Enabled = b And chkBorrarAntVerif.Checked
        btBorrarVerificacion.Enabled = b And chkBorrarVerif.Checked
        If Not chkBorrarVerif.Enabled Then chkBorrarVerif.Checked = False
        If Not chkBorrarAntVerif.Enabled Then chkBorrarAntVerif.Checked = False
        actualizarTablero()
    End Sub

    Private Sub chkBorrarAntVerif_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBorrarAntVerif.CheckedChanged
        btBorrarAntVerif.Enabled = chkBorrarAntVerif.Checked
    End Sub

    Private Sub chkBorrarVerif_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBorrarVerif.CheckedChanged
        btBorrarVerificacion.Enabled = chkBorrarVerif.Checked
    End Sub

    Private Sub btBorrarAntVerif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btBorrarAntVerif.Click
        borrarBolillaVerificada()
        chkBorrarAntVerif.Checked = False
    End Sub

    Private Sub txtVerificarBolilla_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtVerificarBolilla.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            If validarBolillaAVerificar() Then
                actualizarHistorialVerificacion()
                actualizarTablero()
            End If
            txtVerificarBolilla.Text = ""
        End If
    End Sub

    Private Sub btBorrarVerificacion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btBorrarVerificacion.Click
        txtHistorialVerificacion.Text = ""
        actualizarTablero()
        chkBorrarVerif.Checked = False
        chkVerificar.Checked = False
    End Sub

    Private Sub txtNuevaBolilla_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNuevaBolilla.Enter
        chkVerificar.Checked = False
    End Sub
End Class
